#include "bsp.h"

__IO uint32_t TimeDisplay = 0;
static u32 THH, TMM, TSS;

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void RTC_Configuration(void)
{
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Reset Backup Domain */
  BKP_DeInit();

  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {}

  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Enable the RTC Second */
  RTC_ITConfig(RTC_IT_SEC, ENABLE);

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}




//uint32_t Time_Regulate(void)
//{
//  uint32_t Tmp_HH = 0xff, Tmp_MM = 0xff, Tmp_SS = 0xff;

//  printf("\r\n==============Time Settings=====================================");
//  printf("\r\n  Please Set Hours");

//  while (Tmp_HH == 0xFF)
//  {
//    Tmp_HH = USART_Scanf(23);
//  }
//  printf(":  %d", Tmp_HH);
//  printf("\r\n  Please Set Minutes");
//  while (Tmp_MM == 0xFF)
//  {
//    Tmp_MM = USART_Scanf(59);
//  }
//  printf(":  %d", Tmp_MM);
//  printf("\r\n  Please Set Seconds");
//  while (Tmp_SS == 0xFF)
//  {
//    Tmp_SS = USART_Scanf(59);
//  }
//  printf(":  %d", Tmp_SS);

//  /* Return the value to store in RTC counter register */
//  return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
//}

void Time_Adjust(void)
{
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  /* Change the current time */
  RTC_SetCounter(TIME_Regulate());
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}

//void Time_Display(uint32_t TimeVar)
//{
//  uint32_t THH = 0, TMM = 0, TSS = 0;
//  
//  /* Reset RTC Counter when Time is 23:59:59 */
//  if (RTC_GetCounter() == 0x0001517F)
//  {
//     RTC_SetCounter(0x0);
//     /* Wait until last write operation on RTC registers has finished */
//     RTC_WaitForLastTask();
//  }
//  
//  /* Compute  hours */
//  THH = TimeVar / 3600;
//  /* Compute minutes */
//  TMM = (TimeVar % 3600) / 60;
//  /* Compute seconds */
//  TSS = (TimeVar % 3600) % 60;

//  printf("Time: %0.2d:%0.2d:%0.2d\r", THH, TMM, TSS);
//}



void Time_Show(void)
{
  printf("\n\r");

  /* Infinite loop */
  while (1)
  {
    /* If 1s has been elapsed */
    if (TimeDisplay == 1)
    {
      /* Display current time */
      TIME_Display(RTC_GetCounter());
      TimeDisplay = 0;
			sec();
			min();
			hour();
    }
		if(!TPEN())	
		{
			ptask_flag = ptask2;
			break;
		}
  }
}

uint8_t USART_Scanf(uint32_t value)
{
  uint32_t index = 0;
  uint32_t tmp[2] = {0, 0};

  while (index < 2)
  {
    /* Loop until RXNE = 1 */
    while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
    {}
    tmp[index++] = (USART_ReceiveData(USART1));
    if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
    {
      printf("\n\rPlease enter valid number between 0 and 9");
      index--;
    }
  }
  /* Calculate the Corresponding value */
  index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
  /* Checks */
  if (index > value)
  {
    printf("\n\rPlease enter valid number between 0 and %d", value);
    return 0xFF;
  }
  return index;
}

void RTC_IRQHandler(void)
{
  if(RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {
    /* Clear the RTC Second interrupt */
    RTC_ClearITPendingBit(RTC_IT_SEC);

    /* Enable time update */
    TimeDisplay = 1;
		

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    
		
  }
}

void sec(void)
{
	
	GUI_Line(120,160,120+70*cos((TSS)*6*3.14/180+3*3.14/2),160+70*sin((TSS)*6*3.14/180+3*3.14/2),BLUE);
	GUI_Line(120,160,120+70*cos(6*((TSS-1+60)%60)*3.14/180+3*3.14/2),160+70*sin(6*((TSS-1+60)%60)*3.14/180+3*3.14/2),WHITE);
}

void min(void)
{
	
	GUI_Line(120,160,120+60*cos(TMM*6*3.14/180-3.14/2),160+60*sin(TMM*6*3.14/180-3.14/2),BLACK);
	GUI_Line(120,160,120+60*cos(6*((TMM-1+60)%60)*3.14/180-3.14/2),160+60*sin(6*((TMM-1+60)%60)*3.14/180-3.14/2),WHITE);
	
}

void hour(void)
{

	GUI_LineWith(120,160,120+40*cos(30*THH*3.14/180-3.14/2),160+40*sin(THH*30*3.14/180-3.14/2),2,RED);
	GUI_LineWith(120,160,120+40*cos(30*((THH-1+60)%60)*3.14/180-3.14/2),160+40*sin(30*((THH-1+60)%60)*3.14/180-3.14/2),2,WHITE);
}

void TIME_Display(uint32_t TimeVar)
{
	
	u32 time[6];
	
	if (RTC_GetCounter() == 0x0001517F)
  {
     RTC_SetCounter(0x0);
     /* Wait until last write operation on RTC registers has finished */
     RTC_WaitForLastTask();
  }
  
  /* Compute  hours */
  THH = TimeVar / 3600;
  /* Compute minutes */
  TMM = (TimeVar % 3600) / 60;
  /* Compute seconds */
  TSS = (TimeVar % 3600) % 60;
	time[0] = THH /10;
	time[1] = THH %10;
	time[2] = TMM /10;
	time[3] = TMM %10;
	time[4] = TSS /10;
	time[5] = TSS %10;
	show_xXx(88,250,BLACK,WHITE,time[0]*16+zf,16,8);
	show_xXx(96,250,BLACK,WHITE,time[1]*16+zf,16,8);
	show_xXx(112,250,BLACK,WHITE,time[2]*16+zf,16,8);
	show_xXx(120,250,BLACK,WHITE,time[3]*16+zf,16,8);
	show_xXx(138,250,BLACK,WHITE,time[4]*16+zf,16,8);
	show_xXx(146,250,BLACK,WHITE,time[5]*16+zf,16,8);
	
}

uint32_t TIME_Regulate(void)
{
  uint32_t Tmp_HH = 10, Tmp_MM = 59, Tmp_SS = 50;
  return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
}

void GUI_PLAY(void)
{
	
	GUI_CircleFill(120,160,80,WHITE);
	show_xXx(128,250,BLACK,WHITE,64*16+zf,16,8);
	show_xXx(104,250,BLACK,WHITE,64*16+zf,16,8);
}
